$(document).ready(function(){

	if( WSLDS.Utils.isIE6() ){
		showInfoBar('<h2>您正在使用陈旧的Internet Explorer 6!</h2><br/>' +
					'很抱歉,我并没有选择去兼容它!<br/>' +
					'我建议你升级Internet Explorer浏览器或者选择其他浏览器来获得更好的体验.<br/>' +
					'我向您推荐以下几款优秀的浏览器:' +
					'<a style="color:white;" href="http://www.onlinedown.net/soft/65603.htm" >Internet Explorer 8</a>&nbsp;' +
					'<a style="color:white;" href="http://www.mozillaonline.com/" >火狐浏览器</a>&nbsp;' +
					'<a style="color:white;" href="http://www.google.com/chrome" >谷歌浏览器</a>&nbsp;');
	}
	
	/*
		设置 弹出提示	
	*/
	var tipsMap = {
		'left':'e',
		'right':'w',
		'top':'s',
		'bottom':'n'
	};
	$('*[tipsLoc]').each(function(){
		var loc = tipsMap[$(this).attr("tipsLoc")];
		$(this).tips({gravity: loc}); 
	});
	
	/* 设置 input,textarea 获得焦点样式 */
	$('input[needFocus]').bind("focus",function(){
		$(this).addClass("inputFocus");
		$(this).removeClass("inputNormal");
	});
	$('input[needFocus]').bind("blur",function(){
		$(this).removeClass("inputFocus");
		$(this).addClass("inputNormal");
	});
	$('textarea[needFocus]').bind("focus",function(){
		$(this).addClass("inputFocus");
		$(this).removeClass("inputNormal");
	});
	$('textarea[needFocus]').bind("blur",function(){
		$(this).removeClass("inputFocus");
		$(this).addClass("inputNormal");
	});
	
	
	$('#top').bind("click",function(){
		location.href = WSLDS.Utils.homePage();
	});
	
	$('#rss_img').bind("click",function(){
		location.href = WSLDS.Utils.rssPage();
	});
	
	/* MENU START */
	$('.hoverTitle').each(function(){
		var titleName = $(this).text();
		$(this).text("");
		$(this).append(WSLDS.CONS.GET('MENU_HTML'));
		$(this).find('.head_menu_normal_center').text(titleName);		   
	});
	
	$('.hoverTitle').bind("mouseover",function(){
		var divArr = $(this).find("DIV");
		
		$(divArr.get(0)).removeClass("head_menu_normal_left");
		$(divArr.get(1)).removeClass("head_menu_normal_center");
		$(divArr.get(2)).removeClass("head_menu_normal_right");
		
		$(divArr.get(0)).addClass("head_menu_hover_left");
		$(divArr.get(1)).addClass("head_menu_hover_center");
		$(divArr.get(2)).addClass("head_menu_hover_right");
	});
	
	$('.hoverTitle').bind("mouseout",function(){
		var divArr = $(this).find("DIV");
		
		$(divArr.get(0)).removeClass("head_menu_hover_left");
		$(divArr.get(1)).removeClass("head_menu_hover_center");
		$(divArr.get(2)).removeClass("head_menu_hover_right");
		
		$(divArr.get(0)).addClass("head_menu_normal_left");
		$(divArr.get(1)).addClass("head_menu_normal_center");
		$(divArr.get(2)).addClass("head_menu_normal_right");
		
	});
	
	$('.hoverTitle_selected').each(function(){
		var titleName = $(this).text();
		$(this).text("");
		$(this).append(WSLDS.CONS.GET('MENU_SELECTED_HTML'));
		$(this).find('.head_menu_hover_center').text(titleName);		   
	});
	
	
	/* MENU END */
	
	/* PAGER START */
	
	$('.pager_block').each(function(){
		//var curr_pager = $($(this).find("DIV").get(0));		
		var aHtml = $(this).html();
		var pageNum = $(aHtml).text();
		var _href = $(aHtml).attr("href");
		$(this).attr("href",_href);
		$(this).html("");
		$(this).append(WSLDS.CONS.GET('PAGER_NORMAL_HTML'));
		$(this).find('.pager_center').html(pageNum);	
		$(this).find('.pager_left').html("");
		$(this).find('.pager_right').html("");
	});
	
	$('.pager_block').bind("click",function(){
		window.location.href = $(this).attr("href");
	});
	
	$('.pager_block').bind("mouseover",function(){
		var divArr = $(this).find("DIV");
		if( !$(this).find('.pager_center_selected').text() ){
			$(divArr.get(0)).removeClass("pager_left");
			$(divArr.get(1)).removeClass("pager_center");
			$(divArr.get(2)).removeClass("pager_right");
			
			$(divArr.get(0)).addClass("pager_left_hover");
			$(divArr.get(1)).addClass("pager_center_hover");
			$(divArr.get(2)).addClass("pager_right_hover");
		}
		
	});
	
	$('.pager_block').bind("mouseout",function(){
		var divArr = $(this).find("DIV");
		if( !$(this).find('.pager_center_selected').text() ){
			$(divArr.get(0)).removeClass("pager_left_hover");
			$(divArr.get(1)).removeClass("pager_center_hover");
			$(divArr.get(2)).removeClass("pager_right_hover");
			
			$(divArr.get(0)).addClass("pager_left");
			$(divArr.get(1)).addClass("pager_center");
			$(divArr.get(2)).addClass("pager_right");
		}
		
	});
	
	$('.pager_selected').each(function(){
		//var curr_pager = $($(this).find("DIV").get(0));		
		var titleName = $(this).text();
		$(this).text("");
		$(this).append(WSLDS.CONS.GET('PAGER_SELECTED_HTML'));
		$(this).find('.pager_center_selected').text(titleName);		   
	});
	
	
	/* PAGER END */
	
	/* SEARCH AREA START */
	
	$('.search_area').each(function(){
		var searchHtml = $(this).html();
		$(this).html("");
		$(this).append(WSLDS.CONS.GET('SEARCH_HTML'));
		$(this).find('.search_area_center').prepend(searchHtml);
		//针对搜索栏两边的图片作特殊处理
		/*
		if( WSLDS.Utils.isIE() ){
			$('.search_area_left').css({"margin-top":'21px'});
			$('.search_area_right').css({"margin-top":'21px'});
		}else{
			$('.search_area_left').css({"margin-top":'21px'});
			$('.search_area_right').css({"margin-top":'21px'});
		}
		*/
	});
	
	$('img[search_ico]').bind("mouseover",function(){
		$(this).attr("src",contextPath +"/skin/light_blue/images/search_icon_hover.png");
	});
	
	$('img[search_ico]').bind("mouseout",function(){
		$(this).attr("src",contextPath +"/skin/light_blue/images/search_icon.png");
	});
	
	$('img[search_ico]').bind("mousedown",function(){
		$(this).attr("src", contextPath +"/skin/light_blue/images/search_icon_click.png");
	});
	
	$('img[search_ico]').bind("mouseup",function(){
		$(this).attr("src", contextPath +"/skin/light_blue/images/search_icon.png");
	});
	
	$('img[search_ico]').bind("click",function(){
		if( $.trim($('#search_text').val()) === WSLDS.CONS.GET('SEARCH_TXT') ){
		}else{
			var text = $.trim($('#search_text').val());
			var str = "/search?hl=zh-CN&safe=strict&q=" + encodeURI(text) + "+site%3Awslds.com";
			window.open("http://www.google.com.hk" + str );
		}
	});
	
	/* SEARCH AREA END */
	
	/* SEARCH INPUT START */
	$('#search_text').val(WSLDS.CONS.GET('SEARCH_TXT'));
	$('#search_text').addClass("search_font_init");

	$('#search_text').bind("click",function(){
		if( $(this).val() === WSLDS.CONS.GET('SEARCH_TXT') ){
			$(this).val("");
			$(this).removeClass("search_font_init");
			$(this).addClass("search_font_input");
		}
	});
	
	$('#search_text').bind("blur",function(){
		if( $.trim($(this).val()) === "" ){
			$(this).val(WSLDS.CONS.GET('SEARCH_TXT'));
			$(this).removeClass("search_font_input");
			$(this).addClass("search_font_init");
		}
	});
	
	/* SEARCH INPUT END */
	
	/* sidebar START */
	$('.sidebar_title').each(function(){
		var titleName = $(this).text();
		$(this).text("");
		$(this).append(WSLDS.CONS.GET('SIDEBAR_TITLE_HTML'));
		$(this).find('.sidebar_title_center').text(titleName);								  
	});
	
	/* sidebar END */
	
	/* content START */
	$('.content_title').each(function(){
		var titleName = $(this).html();
		$(this).html("");
		$(this).append(WSLDS.CONS.GET('CONTENT_TITLE_HTML'));
		$(this).find('.content_title_center').html(titleName);								  
	});
	
	/* content END */
	
	/* footer content START */
	$('.footer_content').each(function(){
		var footerHtml = $(this).html();
		$(this).html("");
		$(this).append(WSLDS.CONS.GET('FOOTER_CONTENT_HTML'));
		$(this).find('.footer_center').prepend(footerHtml);						  
	});
	/* footer content END */
	
	$("a.anchorLink").anchorAnimate();
	
});

/* 滑动 */
jQuery.fn.anchorAnimate = function(settings) {
 	settings = jQuery.extend({
		speed : 1100
	}, settings);	
	
	return this.each(function(){
		var caller = this
		$(caller).click(function (event) {	
			event.preventDefault()
			var locationHref = window.location.href
			var elementClick = $(caller).attr("href")
			
			var destination = $(elementClick).offset().top;
			$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, settings.speed);
		  	return false;
		})
	})
}
/* 滑动 END */

/* IE6提示 */
function showInfoBar(caption){
	hideInfoBar();
	$(document.body).append("<div id=infoBar><table width=100%><tr nowrap id=infoContent><td width=30></td><td align=center>" + caption + "</td><td width=60 align=left><div id=infoClose>[关闭]</div></td></tr></table></div>");
	$("#infoClose").click(hideInfoBar);
	$("#infoClose").css("border-color","#ffffff");
	$("#infoClose").css("border","7");
	$("#infoClose").css("cursor","hand");
	$("#infoBar").hide();
	$("#infoContent").css("font-weight","bold");
	$("#infoContent").css("font","15px Arial,Verdana,sans-serif;");
	$("#infoContent").css("color","white");
	$("#infoBar").css("background-color","#ff9900");
//	$("#infoBar").css("background-color","red");
	$("#infoBar").css("position","absolute");
    $(window).resize(info_position);
    $(window).scroll(info_position);
	info_position();
	$("#infoBar").slideDown(400); //i like sliding in more than fade in this case. i think it catches the users attention more effectively
	//$("#infoBar").fadeIn(800); //if you like this more remark the previous line

}

function hideInfoBar(){
	$("#infoBar").fadeOut(400)
	$("#infoBar").remove();
}

function info_position() {
	var scrollPosition = $(window).scrollTop();
	$("#infoBar").css("top",scrollPosition +"px");
	$("#infoContent").css("height","45px");
	$("#infoBar").css("left","0px");
	$("#infoBar").css("width",$(window).width());
}
/* IE6提示 END */

window.WSLDS = {
	"CONS" : (function(){
		var SEARCH_TXT = "请输入内容搜索...";
		
		var MENU_HTML = "<div class=\"head_menu_normal_left\" ></div><div class=\"head_menu_normal_center\" ></div><div class=\"head_menu_normal_right\" ></div>";
		
		var MENU_SELECTED_HTML = "<div class=\"head_menu_hover_left\" ></div><div class=\"head_menu_hover_center\" ></div><div class=\"head_menu_hover_right\" ></div>";
		
		var SEARCH_HTML = "<div class=\"search_area_left\" ></div><div class=\"search_area_center\"><img src=\""+ contextPath +"/skin/light_blue/images/search_icon.png\" search_ico=\"search_ico\" /></div><div class=\"search_area_right\" ></div>";
		
		var SIDEBAR_TITLE_HTML = "<div class=\"sidebar_title_left\" ></div><div class=\"sidebar_title_center\" ></div><div class=\"sidebar_title_right\" ></div>";
		
		var CONTENT_TITLE_HTML = "<div class=\"content_title_left\" ></div><div class=\"content_title_center\" ></div><div class=\"content_title_right\" ></div>";
		
		var FOOTER_CONTENT_HTML = "<div class=\"footer_left\" ></div><div class=\"footer_center\" ></div><div class=\"footer_right\" ></div>";
		
		var PAGER_NORMAL_HTML = "<div class=\"pager_left\" ></div><div class=\"pager_center\" ></div><div class=\"pager_right\" ></div>";
		
		var PAGER_SELECTED_HTML = "<div class=\"pager_left_selected\" ></div><div class=\"pager_center_selected\" ></div><div class=\"pager_right_selected\" ></div>";
		
		var _CONSTANT = function(){};

		_CONSTANT.GET = function(name){
			try{
				return eval(name);
			}catch(e){
				return "";
			}
		};
		
		return _CONSTANT;
	})(),
	Utils : (function(){
		var _utils = function(){};
		_utils.homePage = function(){
			return location.protocol + "//" + location.host +"/" + contextPath;
		}
		
		_utils.rssPage = function(){
			return location.protocol + "//" + location.host +"" + contextPath + "/rss";
		}
		
		_utils.openShowModal = function(url, width, height){
			var ops = "dialogHeight:"+height+"px;dialogLeft:(screen.width/2-195/2)px;dialogTop:(screen.height/2-195/2)px;dialogWidth:"
				+width+"px;center:yes;resizable:yes;scroll:yes;status:yes;";
			return window.showModalDialog(url, '', ops);
		}
		
		_utils.isIE6 = function(){
			return !!window.ActiveXObject&&!window.XMLHttpRequest;
		}
		
		_utils.isIE = function(){
			return navigator.userAgent.indexOf('MSIE') >= 0;
		}
		
		_utils.textLoc = function( obj ){
			if( document.selection ){
				var start = 0;
				var rng;
				if(obj.tagName == "TEXTAREA"){	 //如果是文本域
					//IE在用鼠标拖拉的时候会出问题，所以先判断一下	
					if( event.srcElement.createTextRange){
						rng = event.srcElement.createTextRange();
						rng.moveToPoint(event.x,event.y);
						rng.moveStart("character",-event.srcElement.value.length);
						$(obj).data( "_selection", rng.text.length );
					}else{
						$(obj).removeData( "_selection");
					}
				}else{ //输入框
					rng = document.selection.createRange();
					rng.moveStart("character",-event.srcElement.value.length);
					$(obj).data( "_selection", rng.text.length );
				}
			}else if( obj.selectionStart ){
				$(obj).data( "_selection", obj.selectionStart );
			}else{
				$(obj).data( "_selection", "0" );
			}
		}
		
		return _utils;
	})(),
	Method : {
		page : function( path,id ){
			$.ajax({
				 type: "POST",
				 url: contextPath + "/" + path,
				 success: function(msg){
				 	$('#'+id).html(msg);
				 }
			});
		},
		refreshPage : function(path,id){
			$.ajax({
				 type: "POST",
				 url: contextPath + "/" +path,
				 success: function(msg){
				 	$('#'+id).html(msg);
				 }
			});
		}
	},
	GLOBLE : {}
};
